<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

class OrganizationPermissionSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // 创建组织管理相关权限
        $permissions = [
            'view-organizations',
            'create-organizations',
            'edit-organizations',
            'delete-organizations',
            'manage-organization-users',
        ];

        foreach ($permissions as $permission) {
            Permission::create(['name' => $permission]);
        }

        // 为管理员角色分配所有组织管理权限
        $adminRole = Role::where('name', 'admin')->first();
        if ($adminRole) {
            $adminRole->givePermissionTo($permissions);
        }

        // 为编辑角色分配查看组织权限
        $editorRole = Role::where('name', 'editor')->first();
        if ($editorRole) {
            $editorRole->givePermissionTo(['view-organizations']);
        }

        $this->command->info('组织管理权限已创建并分配给相应角色！');
    }
}